<template>
	<!-- 驾驶证认证 -->
	<view class="main">
		<!-- 顶部 -->
		<view class="header">
			<uni-notice-bar background-color="rgb(233 243 247)" color="#2979FF" show-icon
				text="信息仅用于驾驶证认证,士德物流保障您的信息安全" />
		</view>

		<!-- 内容区域 -->
		<uni-forms class="from-style" :model="mytupian">
			<view class="container">
				<view class="c-zi ">
					<uni-section title="请上传驾驶证照片" type="line" titleFontSize="16px">

						<view class="liangzhang">
							<uni-forms-item name="jiashizheng">
								<view class="bbb">
									<uni-file-picker file-mediatype="image" limit="1" file-extname="png,jpg"
										:image-styles="imageStyles" :auto-upload="false" @select="uploadface"
										:value="driverFaceValue">
										<uni-icons type="plus-filled" size="30" color="blue"
											style="padding-left: 15px;"></uni-icons>
										<text>上传主页</text>
									</uni-file-picker>
								</view>
							</uni-forms-item>

							<uni-forms-item name="jiashifan">
								<view class="bbb">
									<uni-file-picker file-mediatype="image" limit="1" file-extname="png,jpg"
										:image-styles="imageStyles" :auto-upload="false" @select="uploadback"
										:value="driverBackValue">
										<uni-icons type="plus-filled" size="30" color="blue"
											style="padding-left: 15px;"></uni-icons><br>
										<text>上传副页</text>
									</uni-file-picker>
								</view>
							</uni-forms-item>
						</view>
					</uni-section>
				</view>

				<view class="c-zi">
					<uni-section title="请上传从业资格证照片" titleFontSize="16px" type="line">
						<view class="yizhang">
							<uni-forms-item name="jiashizige">
								<view class="bbb">
									<uni-file-picker file-mediatype="image" limit="1" file-extname="png,jpg"
										:image-styles="imageStyles" :auto-upload="false" @select="uploadzige"
										:value="driverBackValue">
										<uni-icons type="plus-filled" size="30" color="blue"
											style="padding-left: 15px;"></uni-icons><br>
										<text>上传</text>
									</uni-file-picker>
								</view>
							</uni-forms-item>
						</view>
					</uni-section>
				</view>
			</view>

			<!-- 底部区域 -->
			<view class="bottom">
				<button type="primary" @click="addDriverlicense()" class="button-style">下一步</button>
			</view>

		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imageStyles: {
					width: 185.25,
					height: 110,
				},
				driversLicense: {},
				driverFaceValue: {},
				driverBackValue: {},
				zigezheng: {},
				mytupian: {
					jiashizhengzheng: '',
					jiashizhengfan: '',
					jiashizige: '',
					phone: '17635711209',
				},
			}
		},
		methods: {

			//驾驶证正面上传
			uploadface(e) {
				console.log(111111)
				let that = this;
				let tiemr = new Date();
				let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr
					.getDate();
				address = 'zskp/userHead/' + address + '/';
				var tempFilePaths = e.tempFilePaths[0];
				let str = e.tempFilePaths[0].substr(e.tempFilePaths[0].lastIndexOf('.'));
				let nameStr = address + tiemr.getTime() + str;
				let ossUrl = 'http://localhost:8099/tsDriversLicenseCertification/getDrivingFace';
				uni.uploadFile({
					url: ossUrl, //输入你的bucketname.endpoint
					filePath: tempFilePaths,
					fileType: 'image',
					name: 'file',
					formData: {
						name: nameStr,
						key: nameStr,
						policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wNi0yNlQwMjoyMjozOS4yMjJaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIzLTA2LTI2LyJdXX0=', // 输入你获取的的policy
						OSSAccessKeyId: 'LTAI5tJsR6uZnmEUoa4c5hzH', // 输入你的AccessKeyId
						success_action_status: '200', // 让服务端返回200,不然，默认会返回204
						signature: 'isMKvr1Wrwnd1zSpo/cgvCPKSNY=' // 输入你获取的的signature
					},
					success: res => {
						if (res.statusCode == '200') {
							const result = JSON.parse(res.data)
							console.log(result.data1);
							this.driversLicense.address = result.data1.address
							this.driversLicense.driversLicenseFront = result.data1.driversLicenseFront
							this.driversLicense.endDate = result.data1.endDate
							this.driversLicense.gender = result.data1.gender
							this.driversLicense.issueDate = result.data1.issueDate
							this.driversLicense.issueUnit = result.data1.issueUnit
							this.driversLicense.licenseNumber = result.data1.licenseNumber
							this.driversLicense.name = result.data1.name
							this.driversLicense.startDate = result.data1.startDate
							this.driversLicense.vehicleType = result.data1.vehicleType
						} else {
							uni.showToast({
								title: '上传失败',
								icon: 'none',
								duration: 1000
							})
						}
					}
				})
			},
			//驾驶证反面上传
			uploadback(e) {
				let that = this;
				let tiemr = new Date();
				let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr
					.getDate();
				address = 'zskp/userHead/' + address + '/';
				var tempFilePaths = e.tempFilePaths[0];
				let str = e.tempFilePaths[0].substr(e.tempFilePaths[0].lastIndexOf('.'));
				let nameStr = address + tiemr.getTime() + str;
				let ossUrl = 'http://localhost:8099/tsDriversLicenseCertification/getDrivingBack';
				uni.uploadFile({
					url: ossUrl, //输入你的bucketname.endpoint
					filePath: tempFilePaths,
					fileType: 'image',
					name: 'file',
					formData: {
						name: nameStr,
						key: nameStr,
						policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wNi0yNlQwMjoyMjozOS4yMjJaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIzLTA2LTI2LyJdXX0=', // 输入你获取的的policy
						OSSAccessKeyId: 'LTAI5tJsR6uZnmEUoa4c5hzH', // 输入你的AccessKeyId
						success_action_status: '200', // 让服务端返回200,不然，默认会返回204
						signature: 'isMKvr1Wrwnd1zSpo/cgvCPKSNY=' // 输入你获取的的signature
					},
					success: res => {
						if (res.statusCode == '200') {
							const result = JSON.parse(res.data)
							console.log(result.data1);
							this.driversLicense.archiveNumber = result.data1.archiveNumber
							this.driversLicense.driversLicenseOpposite = result.data1.driversLicenseOpposite
							this.driversLicense.record = result.data1.record
							this.driversLicense.vehicleType = result.data1.vehicleType
						} else {
							uni.showToast({
								title: '上传失败',
								icon: 'none',
								duration: 1000
							})
						}
					}
				})
			},
			// 驾驶资格上传
			uploadzige(e) {
				let that = this;
				let tiemr = new Date();
				let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr
					.getDate();
				address = 'zskp/userHead/' + address + '/';
				var tempFilePaths = e.tempFilePaths[0];
				let str = e.tempFilePaths[0].substr(e.tempFilePaths[0].lastIndexOf('.'));
				let nameStr = address + tiemr.getTime() + str;
				let ossUrl = 'http://localhost:8099/tsDriversLicenseCertification/getDrivingzige';
				uni.uploadFile({
					url: ossUrl, //输入你的bucketname.endpoint
					filePath: tempFilePaths,
					fileType: 'image',
					name: 'file',
					formData: {
						name: nameStr,
						key: nameStr,
						policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wNi0yNlQwMjoyMjozOS4yMjJaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIzLTA2LTI2LyJdXX0=', // 输入你获取的的policy
						OSSAccessKeyId: 'LTAI5tJsR6uZnmEUoa4c5hzH', // 输入你的AccessKeyId
						success_action_status: '200', // 让服务端返回200,不然，默认会返回204
						signature: 'isMKvr1Wrwnd1zSpo/cgvCPKSNY=' // 输入你获取的的signature
					},
					success: res => {
						if (res.statusCode == '200') {
							const result = JSON.parse(res.data)
							console.log(result.data1);
							this.driversLicense.certificatePhoto = result.data1
						} else {
							uni.showToast({
								title: '上传失败',
								icon: 'none',
								duration: 1000
							})
						}
					}
				})
			},
			// 将驾驶证信息注册为司机用户
			async addDriverlicense() {
				console.log(11111)
				const result = await uni.$http.post(
					"localhost:8099/tsDriversLicenseCertification/insertDriverLicense",
					this.driversLicense);
				console.log(this.driversLicense)
				const {
					data,
					code
				} = result.data
				if (code == 0) {
					console.log("添加成功")
					this.myzhongxin()
				} else if (code == 101) {
					uni.showToast({
						title: "信息填写错误，请重新填写信息",
						icon: 'none',
						duration: 1000
					});
				} else if (code == 102) {
					uni.showToast({
						title: "账号已存在，请确认信息",
						icon: 'none',
						duration: 1000
					});
				}
			},

			// 跳转到个人中心
			myzhongxin() {
				console.log(111111)
				if (this.driversLicense == null) {
					return
				} else {
					uni.switchTab({
						url: '/pages/mine/mine'
					});
				}
			},

		}
	}
</script>

<style>
	/* 最大容器 */
	.main {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
		background-color: Snow;
	}

	/* 头部容器 */
	.header {
		width: 100%;
		height: 100rpx;
		margin-bottom: -10px;
	}

	/* 中间容器 */
	.container {
		display: flex;
		flex-flow: column nowrap;
		justify-content: flex-start;
		align-content: space-evenly;
		width: 100%;
		height: 686px;
		overflow-y: scroll;
	}

	.c-zi {
		width: 95%;
		/* flex: 1; */
		margin: 0px auto;
		margin-bottom: -22px;
	}

	.yizhang {
		display: flex;
		width: 50%;
		height: 120px;
		margin: 0 5px;
	}

	.liangzhang {
		display: flex;
		width: 100%;
		height: 120px;
		justify-content: space-evenly;
	}

	/* 底部容器 */
	.bottom {
		width: 100%;
		height: 120rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/* 下一步按钮样式 */
	.button-style {
		margin: 0 auto;
		width: 80%;
		border-radius: 35px;
	}

	.aaa {
		display: flex;
		width: 100%;
		height: 100%;
		justify-content: center;
		align-items: center;
		background-color: whitesmoke;
		/* padding: 10px;
		margin: 0 5px; */
	}

	.bbb {
		display: flex;
		flex-flow: column nowrap;
		justify-content: center;
		align-items: center;
		background-color: whitesmoke;
	}
</style>